﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using BLL.BussinessInterface;
namespace BLL.BussinessClass
{
    public class UserBiz : IUserBiz
    {
        #region IUserBiz 成员

        public List<USER> GetUserList()
        {
            UserRepository userRepo = UserRepository.Instrance;
            return userRepo.LoadUserXML();
        }

        public USER GetUser(string id)
        {
            UserRepository userRepo = UserRepository.Instrance;
            List<USER> userList = userRepo.LoadUserXML();
            if (null == userList)
            {
                return null;
            }
            return userList.FirstOrDefault(c => c.ID == id);
        }

        public bool AddUser(USER user)
        {
            UserRepository userRepo = UserRepository.Instrance;
            return userRepo.AddUser(user);
        }

        public List<USER> GetUserList(USER user)
        {
            UserRepository userRepo = UserRepository.Instrance;
            List<USER> userList = userRepo.LoadUserXML();
            if (null == userList)
            {
                return new List<USER>();
            }

            if (!string.IsNullOrEmpty(user.ID))
            {
                userList = userList.Where(c => c.ID == user.ID).ToList<USER>();
            }
            if (!string.IsNullOrEmpty(user.NAME))
            {
                userList = userList.Where(c => c.NAME == user.NAME).ToList<USER>();
            }
            if (user.RIGHT >= 0)
            {
                userList = userList.Where(c => c.RIGHT == user.RIGHT).ToList<USER>();
            }

            return userList;
        }

        public bool UpdateUser(USER user)
        {
            if (string.IsNullOrEmpty(user.ID))
            {
                return false;
            }
            UserRepository userRepo = UserRepository.Instrance;
            return userRepo.UpdateUser(user);
        }

        #endregion
    }
}
